import { rawRequestClient } from '#/api/request';

// 模型请求记录接口类型定义
export interface ModelRequestRecord {
  id?: string;
  request_id: string;
  parent_request_id?: string;
  agent_id?: string;
  chat_id?: string;
  channel_id: number;
  channel_account_id: number;
  user_id?: string;
  model_name: string;
  is_success: boolean;
  error_code?: string;
  error_message?: string;
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  cost: number;
  request_data?: string;
  response_data?: string;
  created_at?: string;
  created_by?: string;
}

// 渠道账号统计项接口类型定义
export interface ChannelAccountStatItem {
  channel_account_id: number;
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  total_cost: number;
  used_count: number;
  last_used_at?: string;
}

// 渠道账号统计结果接口类型定义
export interface ChannelAccountStats {
  account_count: number;
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  total_cost: number;
  avg_tokens: number;
  avg_cost: number;
  avg_used_count: number;
  items: ChannelAccountStatItem[];
}

// 获取模型请求记录列表
export function getModelRequestRecordList(params: {
  page?: number;
  size?: number;
  request_id?: string;
  parent_request_id?: string;
  agent_id?: string;
  chat_id?: string;
  channel_id?: number;
  channel_account_id?: number;
  user_id?: string;
  model_name?: string;
  is_success?: boolean;
  error_code?: string;
  start_time?: string;
  end_time?: string;
}) {
  return rawRequestClient.get(`${import.meta.env.VITE_MODEL_LAKE_SERVER}/management/model-request-records`, { params });
}

// 获取渠道账号统计信息
export function getChannelAccountStats(params: {
  channel_id?: number;
  start_time?: string;
  end_time?: string;
  order_by?: 'total_tokens' | 'total_cost' | 'last_used_at' | 'used_count';
  order_dir?: 'asc' | 'desc';
}) {
  return rawRequestClient.get(`${import.meta.env.VITE_MODEL_LAKE_SERVER}/management/model-request-records/channel-account-stats`, { params });
}
